package com.base.thread.demo_19;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 序列生成器
 * @ClassName: Sequence
 * @Description: 
 * @Author xuyou
 * @DateTime 2019年12月7日 下午4:07:03
 */
public class Sequence {
	
	private AtomicInteger value = new AtomicInteger(0);
	
	private int[] s = {2,4,5,9};
	
	AtomicIntegerArray array = new AtomicIntegerArray(s);
	
	AtomicReference<User> t = new AtomicReference<>();
	
	User user = new User();
	
	AtomicIntegerFieldUpdater<User> old = AtomicIntegerFieldUpdater.newUpdater(User.class, "old");
	
	public int getNext() {
		
//		int get = array.addAndGet(2, 10);
//		System.out.println(s[2]);
//		return get;
		//加一
//		return value.getAndIncrement();
		
		return old.getAndIncrement(user);
		
		
	}
	
	
	public static void main(String[] args) {
		
		Sequence s = new Sequence();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					System.out.println(Thread.currentThread().getName() + " " + s.getNext());
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					System.out.println(Thread.currentThread().getName() + " " + s.getNext());
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true) {
					System.out.println(Thread.currentThread().getName() + " " + s.getNext());
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		
	}

}
